package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.system.mapper.LabProductCasMapper;
import com.ruoyi.labdata.domain.LabTwoDimensionalCode;
import com.ruoyi.labdata.mapper.LabTwoDimensionalCodeMapper;
import com.ruoyi.system.domain.LabProductCas;
import com.ruoyi.system.service.ILabProductCasService;

/**
 * 申请采购明细Service业务层处理
 * 
 * @author winner
 * @date 2022-03-23
 */
@Service
public class LabProductCasServiceImpl implements ILabProductCasService {
	@Autowired
	private LabProductCasMapper labProductCasMapper;
	@Autowired
	private LabTwoDimensionalCodeMapper twoDimensionalCodeMapper;

	/**
	 * 查询申请采购明细
	 * 
	 * @param productId
	 *            申请采购明细ID
	 * @return 申请采购明细
	 */
	@Override
	public LabProductCas selectLabProductCasById(Long productId) {
		return labProductCasMapper.selectLabProductCasById(productId);
	}

	/**
	 * 查询申请采购明细列表
	 * 
	 * @param labProductCas
	 *            申请采购明细
	 * @return 申请采购明细
	 */
	@Override
	public List<LabProductCas> selectLabProductCasList(LabProductCas labProductCas) {
		return labProductCasMapper.selectLabProductCasList(labProductCas);
	}

	/**
	 * 新增申请采购明细
	 * 
	 * @param labProductCas
	 *            申请采购明细
	 * @return 结果
	 */
	@Override
	public int insertLabProductCas(LabProductCas labProductCas) {
		return labProductCasMapper.insertLabProductCas(labProductCas);
	}

	/**
	 * 修改申请采购明细
	 * 
	 * @param labProductCas
	 *            申请采购明细
	 * @return 结果
	 */
	@Override
	public int updateLabProductCas(LabProductCas labProductCas) {
		return labProductCasMapper.updateLabProductCas(labProductCas);
	}

	/**
	 * 批量删除申请采购明细
	 * 
	 * @param productIds
	 *            需要删除的申请采购明细ID
	 * @return 结果
	 */
	@Override
	public int deleteLabProductCasByIds(Long[] productIds) {
		return labProductCasMapper.deleteLabProductCasByIds(productIds);
	}

	/**
	 * 删除申请采购明细信息
	 * 
	 * @param productId
	 *            申请采购明细ID
	 * @return 结果
	 */
	@Override
	public int deleteLabProductCasById(Long productId) {
		return labProductCasMapper.deleteLabProductCasById(productId);
	}

	public int scanoutIventory(LabProductCas labProductCas) {
		return this.labProductCasMapper.scanoutIventory(labProductCas);
	}

	public int scaninIventory(LabProductCas labProductCas) {
		return this.labProductCasMapper.scaninIventory(labProductCas);
	}

	public List<LabProductCas> casInventoryList(LabProductCas labProductCas) {
		return this.labProductCasMapper.casInventoryList(labProductCas);
	}

	public List<LabProductCas> casInventoryHistoryList(LabProductCas labProductCas) {
		return this.labProductCasMapper.casInventoryHistoryList(labProductCas);
	}

	@Override
	@Transactional
	public int updateQrCode(Long productId, String qrCode) {
		LabProductCas product = this.selectLabProductCasById(productId);
		if(product==null){
			throw new RuntimeException("该产品编号不存在.");
		}
		product.setQrCode(qrCode);
		int ret = this.labProductCasMapper.updateLabProductCas(product);
		LabTwoDimensionalCode labTwoDimensionalCode = twoDimensionalCodeMapper.selectLabTwoDimensionalCodeByCode(qrCode);
		if(labTwoDimensionalCode==null){
			throw new RuntimeException("该二维码不存在");
		}
		labTwoDimensionalCode.setIsBinding("1");
		labTwoDimensionalCode.setBingGoodsName(product.getMaterialName()+"("+product.getCasNo()+")");
		twoDimensionalCodeMapper.updateLabTwoDimensionalCode(labTwoDimensionalCode);
		return ret;
	}
	
}
